package com.etc.servlet.user;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.SessionListener;

import com.etc.common.DBUtil;

@WebServlet("/user/login.action")
public class LoginServlet extends HttpServlet{

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 获取参数列表中的参数
		
//		req.getHeader("")
		
		String ran = req.getParameter("random");
		// 验证码比较
		HttpSession session = req.getSession();
		String random = String.valueOf(session.getAttribute("random"));
		if (!random.equalsIgnoreCase(ran)) {
			// 验证码不正确
			req.setAttribute("msg", "验证码不正确");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
			return;
		}
		
		
		System.out.println(req.getHeader("Content-Type"));
		System.out.println(req.getHeader("Origin"));
		System.out.println(req.getHeader("Referer")); // 防盗链
		System.out.println(req.getHeader("UserAgen")); // 客户端浏览器
		
		System.out.println(req.getRealPath("")); // 项目目录
		System.out.println(req.getContextPath()); // 请求工程路径
		System.out.println(req.getServletPath()); // 请求
		
		// getParameter("") 获取参数列表
		// String[] str = getParameterValues(""); 获取 多个同名的参数列表
		String uname = req.getParameter("uname");
		String psd = req.getParameter("password");
		
		// ||   true || 不执行
		if (psd == null || psd.trim().equals("")) { // "".equals(psd.trim())
			resp.setContentType("text/html;charset=UTF-8");
			resp.getWriter().write("<html><script>alert(\"密码必须输入\")</script></html>");
			return;
		}
		
		// 连接数据库 
		Connection conn = DBUtil.getConnection();
		StringBuilder sb = new StringBuilder();
		Map<String, Object> map = new HashMap<>();
		sb.append(" SELECT id, uname, psd ");
		sb.append(" FROM userinfo_mst     ");
		sb.append(" WHERE uname = ?       ");
		try {
			PreparedStatement ps = conn.prepareStatement(sb.toString());
			
			ps.setString(1, uname);
			
			ResultSet rs = ps.executeQuery();
			
			while (rs.next()) {
				map.put("id", rs.getInt(1));
				map.put("uname", rs.getString(2));
				map.put("psd", rs.getString(3));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		// 逻辑判断
		if (map.size() == 0) { // 用户名不存在
			// 用户名不存在
			req.setAttribute("msg", "用户名不存在");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
//			
		} else if (!map.get("psd").equals(psd)){ // 用户名存在， 密码不正确
//			// 用户名和密码不正确
			req.setAttribute("msg", "用户名存在， 密码不正确");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		} else { // 成功了吧
			
			
			// 记住我
			String remem = req.getParameter("rememberme");
			if (remem != null && "1".equals(remem)) {
				
				Cookie cookie = new Cookie("uname", String.valueOf(map.get("uname")));
				resp.addCookie(cookie);
			}
			
			// 登录成功
//			// 重定向
			session.setAttribute("uname", map.get("uname"));
			resp.sendRedirect("welcome.jsp");
		}
		
	}


}
